import zlib
import string
import copy
import struct
import time
import hashlib

def standard(data):
    star_list = data["stars"]
    def addname(e):
        m = hashlib.md5()
        m.update(struct.pack("fffsI", e["x"], e["y"], e["z"], e["class"], int(e["temperature"])))
        e["name"] = m.hexdigest()
        return e
    map(addname, star_list)

def cmp_function(test_f, data):
    data2 = copy.deepcopy(data)
    start = time.clock()
    test_f(data2)
    end = time.clock()
    dt1 = end-start

    start = time.clock()
    standard(data)
    end = time.clock()
    dt2 = end-start    
    
    for sid, star in enumerate(data["stars"]):
        if data2["stars"][sid]["name"] != star["name"]:
            raise Exception("star_list compare failed at index "+str(sid)+": expected "+star["name"]+", but user list has "+data2["stars"][sid]["name"])

    print "submitted algorithm time:", dt1
    print "standard algorithm time:", dt2
    return True

def run_function(data):
    start = time.clock()
    standard(data)
    end = time.clock()
    dt2 = end-start    
    
    print "star[0] name:", data["stars"][0]["name"]

    print "standard algorithm time:", dt2
    return True
